Golang encoding/xml.Encoder.Flush() function example
package encoding/xml
Flush flushes any buffered XML to the underlying writer. See the EncodeToken documentation for details about when it is necessary.
Golang encoding/xml.Encoder.Flush() function usage example
package main
import (
"encoding/xml"
"fmt"
"os"
"reflect"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
newtoken := xml.StartElement{
Name : xml.Name{
Space: "",
Local: "location",
},
}
if err := encoder.EncodeToken(newtoken); err != nil {
fmt.Println(err)
}
start := xml.StartElement{
Name: xml.Name{
Space: "",
Local: reflect.Indirect(reflect.ValueOf(v)).Type().Name(),
},
Attr: []xml.Attr{{xml.Name{"", "xmlns"}, "https://socketloop.com/xmldoc/2014-09-01/"}},
}
if err := encoder.EncodeElement(v, start); err != nil {
fmt.Println(err)
}
encoder.Flush() // <-- here
}
Reference :
Advertisement
Something interesting
Tutorials
+16.5k Golang : Check if a string contains multiple sub-strings in []string?
+36k Golang : Get file last modified date and time
+14.2k Golang : Fix image: unknown format error
+8.9k Golang : Sort lines of text example
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+30.4k Golang : How to verify uploaded file is image or allowed file types
+30.4k Golang : How to redirect to new page with net/http?
+5.3k Golang : How to deal with configuration data?
+14.7k Golang : Reset buffer example
+12.8k Golang : Listen and Serve on sub domain example
+6.5k Unix/Linux : How to get own IP address ?
+5k Golang : micron to centimeter example